package com.wwwc.index.web.servlet;

import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;
import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;
import com.wwwc.index.web.ejb.*;

public class AdvBean {
    private EJBDatabaseQuery ejbdb;

    public StringBuffer advRequiredForm(HttpServletRequest request, String sid, String pid, String call_path) {
	StringBuffer sbf   = new StringBuffer();
	StringBuffer error = null;
	MyUserInputCheck ck = new MyUserInputCheck();

	String company     = request.getParameter("company");
	String company_url = request.getParameter("company_url");
	String industry    = request.getParameter("industry");
	String budget      = request.getParameter("budget");

	String address1    = request.getParameter("address1");
	String address2    = request.getParameter("address2");
	String city        = request.getParameter("city");
	String state       = request.getParameter("state");
	String zip         = request.getParameter("zip");
	String country     = request.getParameter("country");

	String contact     = request.getParameter("contact");
	String email       = request.getParameter("email");
	String phone       = request.getParameter("phone");
	String message     = request.getParameter("message");

	if(company != null && company.length() <2) {
	    error = new StringBuffer("Please enter your [Company Name]..");
	}

	if(error == null && company_url != null && company_url.length()>0) {
	    if(!ck.isUrl(company_url)) {
		error = new StringBuffer("[Company URL] error.");
	    }
	}

	if(error == null && industry != null && industry.length()==0) {
	    error = new StringBuffer("Please select your company's [Industry].");
	}

	if(error == null && budget != null && budget.length()==0) {
	    error = new StringBuffer("Please select your company's [Advertising budget].");
	}

	if(error == null && address1 != null && address1.length()==0) {
	    error = new StringBuffer("Please enter your [Address].");
	}
	if(error == null && city != null && city.length()<3) {
	    error = new StringBuffer("Please enter your [City].");
	}
	if(error == null && state != null && state.length()==0) {
	    error = new StringBuffer("Please select your [State].");
	}
	if(error == null && zip != null) {
	    if(!ck.checkZipCode(zip)) {
		error = ck.getMessage();
	    }
	}
	if(error == null && contact != null && contact.length()<1) {
	    error = new StringBuffer("Field [Contact Name] is required.");
	}

	if(error == null && email != null) {
	    if(email.length()<1) {
		error = new StringBuffer("Field [E-mail] is required.");
	    }

	    if(error == null && !ck.checkEmail(email)) {
		error = new StringBuffer("[E-mail] format error.");
	    }
	}

	if(error == null && phone != null) {
	    if(phone.length()<1) {
		error = new StringBuffer("Field [Phone] is required.");
	    }
	    if(error == null && !ck.checkPhoneNumber(phone)) {
		error = new StringBuffer("[Phone] format error.");
	    }
	}
	if(error == null && message != null && message.length()>2000) {
	    error = new StringBuffer("Additional message is too long.");
	}

	if(error == null && email != null && phone != null) {
	    String euser   = "Verification";
	    String epass   = "LiNguyen5788";
	    String from    = "Advertising@3wcenter.com";
	    String to      = "William.Li@3wcenter.com";
	    String subject = "Adv[sid="+sid+"][Position="+pid+"]";

	    String content ="Contact Name:"+contact+"\nE-mail:"+email+"\nPhone:"+phone+"\n\n"+ 
		"Company:"+company+"\nURL:"+company_url+"\nIndustry:"+industry+"\nBudget:"+budget+
		"\n\nAddress:"+address1+"\n"+address2+"\nCity:"+city+"\nState:"+state+"\nZip Code:"+zip+"\nCountry:"+country+
		"\n\n"+message;
	    
	    try {
		SendMail smail = new SendMail();
		if(!smail.send(from, euser, epass, to, subject, content)) {
		    error = new StringBuffer("System Error. Please try again.");
		}
		else {
		    if (ejbdb == null) {
			ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
		    }

		    String query = "INSERT INTO COMPANYS (ID, NAME, URL, INDUSTRY, ADDRESS1, ADDRESS2, CITY, STATE, ZIP,"+
			"COUNTRY, CONTACT, E_MAIL, PHONE)"+
			"VALUES(null, '"+company+"','"+company_url+"','"+industry+
			"','"+address1+"','"+address2+"','"+city+"','"+state+"', '"+zip+
			"','"+country+"','"+contact+"','"+email+"','"+phone+"');";
		    ejbdb.insert(query);

		    sbf.append("<BR><BR><BR><H2>Thank you!</H2><BR><H3>3WCenter.com sales person will contact you ASAP.</H3>");
		    HttpSession session = request.getSession(true);
		    session.setAttribute("adv_submit", request.getRemoteAddr());
		    return sbf;
		}
	    }
	    catch (Exception e) {
		System.out.println("AdvBean:companyForm:Error:["+e+"]");
		error = new StringBuffer("System Error. Please try again.");
	    }
	}

	sbf.append("<CENTER>");
	sbf.append("\n<FORM method='post' action='"+call_path+"'>");
	sbf.append("\n<TABLE width='100%' border=0>");
	sbf.append("<TR>");
	sbf.append("\n<TR><TD align='center' colspan='2'>");
	sbf.append("<h2>This form is for new sales requests.</h2>");
	sbf.append("</TD></TR>");

	if(error != null && error.length()>0) {
	    sbf.append("\n<TR><TD style='font-size: 10pt' align='center' colspan='2'>");
	    sbf.append("\n<img src='/images/star.gif' border='0'><I>"+error+"</I></TD></TR>");
	}

	sbf.append("<TR><TD colspan='2'>&nbsp;</TD></TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Company Name (*):</B></TD>");

	if(company == null) {
	    sbf.append("\n<TD><input type='text' name='company' value='' size='40' maxLength='250'></TD>");
	}
	else {
	    sbf.append("\n<TD><input type='text' name='company' value=\""+company+"\" size='40' maxLength='250'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Company URL:</B></TD>");

	if(company_url == null) {
	    sbf.append("<TD><input type='text' name='company_url' value='' size='40' maxLength='250'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='company_url' value=\""+company_url+"\" size='40' maxLength='250'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Industry (*):</B></TD>");
	sbf.append("<TD>");
	sbf.append(MyHtml.getAdvIndustry("industry", industry));
	sbf.append("</TD>");
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Advertising budget (*):</B></TD>");
	sbf.append("<TD>");
	sbf.append(MyHtml.getAdvBudget("budget", budget));
	sbf.append("</TD>");
	sbf.append("\n</TR>");

	sbf.append("<TR><TD colspan='2'>");
	sbf.append("&nbsp;");
	sbf.append("</TD></TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Address (*):</B></TD>");

	if(address1 == null) {
	    sbf.append("<TD><input type='text' name='address1' value='' size='40' maxLength='250'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='address1' value=\""+address1+"\" size='40' maxLength='250'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD>&nbsp;</TD>");

	if(address2 == null) {
	    sbf.append("<TD><input type='text' name='address2' value='' size='40' maxLength='250'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='address2' value=\""+address2+"\" size='40' maxLength='250'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>City (*):</B></TD>");

	if(city == null) {
	    sbf.append("<TD><input type='text' name='city' value='' size='40' maxLength='250'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='city' value=\""+city+"\" size='40' maxLength='250'></TD>");
	}
	sbf.append("\n</TR>");
	
	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>State (*):</B></TD>");
	sbf.append("<TD>");
	sbf.append(MyHtml.getUsState("state", state));
	sbf.append("</TD>");
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Zip Code (*):</B></TD>");

	if(zip == null) {
	    sbf.append("<TD><input type='text' name='zip' value='' size='20' maxLength='20'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='zip' value=\""+zip+"\" size='20' maxLength='20'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Country (*):</B></TD>");
	sbf.append("<TD>");
	sbf.append(MyHtml.getCountryOption("country", country));
	sbf.append("</TD>");
	sbf.append("\n</TR>");

	sbf.append("<TR><TD colspan='2'>");
	sbf.append("&nbsp;");
	sbf.append("</TD></TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Contact Name (*):</B></TD>");

	if(contact == null) {
	    sbf.append("<TD><input type='text' name='contact' value='' size='40' maxLength='50'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='contact' value=\""+contact+"\" size='40' maxLength='50'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>E-mail (*):</B></TD>");

	if(email == null) {
	    sbf.append("<TD><input type='text' name='email' value='' size='40' maxLength='50'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='email' value=\""+email+"\" size='40' maxLength='50'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD align='right'><B>Phone (*):</B></TD>");

	if(phone == null) {
	    sbf.append("<TD><input type='text' name='phone' value='' size='40' maxLength='50'></TD>");
	}
	else {
	    sbf.append("<TD><input type='text' name='phone' value=\""+phone+"\" size='40' maxLength='50'></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("<TR><TD colspan='2'>");
	sbf.append("&nbsp;");
	sbf.append("</TD></TR>");

	sbf.append("<TR><TD colspan='2' align='center'>");
	sbf.append("Additional message:");
	sbf.append("</TD></TR>");

	sbf.append("\n<TR>");
	sbf.append("\n<TD colspan='2' align='center'>");
	if(message == null) {
	    sbf.append("\n<TEXTAREA name='message' rows=5 cols=38 value=''></TEXTAREA></TD>");
	}
	else {
	    sbf.append("\n<TEXTAREA name='message' rows=5 cols=38>"+message+"</TEXTAREA></TD>");
	}
	sbf.append("\n</TR>");

	sbf.append("<TR><TD colspan='2' align='center'>");
	sbf.append("<input type='submit' name='B' value='Submit' style='font-size: 8pt'>");
	sbf.append("</TD></TR>");
	sbf.append("\n</TABLE>");
	sbf.append("\n<input type='hidden' name='adv_sid' value='"+sid+"'>");
	sbf.append("\n<input type='hidden' name='adv_position' value='"+pid+"'>");
	sbf.append("\n<input type='hidden' name='mid' value='adv'>");
	sbf.append("\n</FORM>");
	sbf.append("</CENTER>");
	return sbf;
    }
}
